package com.dianrui.moonfire.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.moonfire.R;
import com.dianrui.moonfire.activity.HelpCateListActivity;
import com.dianrui.moonfire.bean.HelpCate;
import com.dianrui.moonfire.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HelpCateAdapter extends BaseAdapter {

    private List<HelpCate> list;
    private Context context;
    private int clickTemp =0;


    public HelpCateAdapter(Context context, List<HelpCate> list) {
        this.list = list;
        this.context = context;

    }

    public void refresh(List<HelpCate> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setSeclection(int position) {
        clickTemp = position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_help_grid, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list &&list.size() > 0) {
            final HelpCate helpCate = list.get(position);
            GlideUtil.loadImageView(context.getApplicationContext(),helpCate.logo,holder.helpCateLogo);
            holder.helpCateName.setText(helpCate.name);
            holder.click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, HelpCateListActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("title",helpCate.name);
                    bundle.putString("cate_id",helpCate.cate_id);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });

        }
        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.help_cate_logo)
        ImageView helpCateLogo;
        @BindView(R.id.help_cate_name)
        TextView helpCateName;
         @BindView(R.id.click_btn)
        LinearLayout click;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
